## Makefile to replicate "Reducing Model Misspecification
## and Bias in the Estimation of Interactions"
## Matt Blackwell and Michael Olson
## 2020-10-23

OUTDIR = output
FIGDIR = figures
SRC = source

R_OPTS = --vanilla --no-save


R2Y := 0 0.25 0.5 0.75
R2D := 0 0.25 0.5 0.75
R2Y_B := 0 0.25 0.5 0.75
R2D_B := 0 0.25 0.5 0.75
NSIZE := 425 1000
NSIMS = 1000
NCOV := 20 200

main := $(foreach RY,$(R2Y), $(foreach RD, $(R2D), \
	$(foreach NC, $(NCOV),  $(OUTDIR)/main_sim_425_$(RY)_$(RD)_$(NC).csv)))

largen := $(foreach RY,$(R2Y), $(foreach RD, $(R2D), \
	$(foreach NC, $(NCOV),  $(OUTDIR)/main_sim_1000_$(RY)_$(RD)_$(NC).csv)))

dense := $(foreach RY,$(R2Y), $(foreach RD, $(R2D), \
	$(foreach NC, $(NCOV),  $(OUTDIR)/dense_sim_425_$(RY)_$(RD)_$(NC).csv)))

binary := $(foreach RY,$(R2Y_B), $(foreach RD, $(R2D_B), \
	$(foreach NC, $(NCOV),  $(OUTDIR)/interaction_binary_750_$(RY)_$(RD)_$(NC).csv)))


direct_primary := $(FIGDIR)/primary_plot.pdf $(FIGDIR)/primary_plot_additional.pdf
remittances := $(FIGDIR)/remittances_coefplot.pdf $(FIGDIR)/remittances_coefplot_additional.pdf

.PHONY: all main main_figs binary dense largen binary_figs direct_primary remittances

all: mkdirs
main: ${main}
largen: $(largen)
binary: $(binary)
dense: $(dense)
direct_primary: $(direct_primary)
remittances: $(remittances)

setup:
	R CMD BATCH $(R_OPTS) 00_setup_packages.R

${main}: $(OUTDIR)
	R CMD BATCH $(R_OPTS) "--args 0 425 $(wordlist 4, 6, $(subst _, , $(subst .csv,, $@))) $(NSIMS) main" main_simulations.R

${dense}: $(OUTDIR)
	R CMD BATCH $(R_OPTS) "--args 0 425 $(wordlist 4, 6, $(subst _, , $(subst .csv,, $@))) $(NSIMS) dense" main_simulations.R

${largen}: $(OUTDIR)
	R CMD BATCH $(R_OPTS) "--args 0 1000 $(wordlist 4, 6, $(subst _, , $(subst .csv,, $@))) $(NSIMS) main" main_simulations.R


main_figs: $(FIGDIR)
	R CMD BATCH $(R_OPTS) main_simulations_plots.R


$(binary): $(OUTDIR)
	R CMD BATCH $(R_OPTS) "--args 0 750 $(wordlist 4, 6, $(subst _, , $(subst .csv,, $@))) $(NSIMS) binary" binary_simulations.R

binary_figs: 
	R CMD BATCH $(R_OPTS) binary_simulations_plots.R

${direct_primary}: $(FIGDIR)
	R CMD BATCH $(R_OPTS) direct_primary.R

${remittances}: $(FIGDIR)
	R CMD BATCH $(R_OPTS) remittances.R

$(OUTDIR):
	@echo "Making outdir..."
	mkdir -p $(OUTDIR)

$(FIGDIR):
	@echo "Making figdir..."
	mkdir -p $(FIGDIR)

clean:
	@echo "Cleaning up..."
	rm *.Rout
